﻿/**
 * 时间日期初始化
 */
function dateTimeInit(){
	
    //$('dateLabel').innerText = new Date().format("yyyy.MM.dd");
	try{
		var d = new Date();
		var h = d.getHours();
		var i = d.getMinutes().toString();
		i = i.length<2?'0'+i:i;
		var m = h > 12 ? "PM" : "AM";
		var timeFormat =new Date().format("yyyy.MM.dd")+"  "+ m + " " + h + ":" + i;
		document.getElementById('dateLabel').innerText = timeFormat;
		setInterval(function(){
			var d = new Date();
			var h = d.getHours();
			var i = d.getMinutes().toString();
			i = i.length<2?'0'+i:i;
			var m = h > 12 ? "PM" : "AM";
			var timeFormat =new Date().format("yyyy.MM.dd")+"  "+ m + " " + h + ":" + i;
			document.getElementById('dateLabel').innerText = timeFormat;
		}, 1000);
		var week;
		switch (d.getDay()){
		  case 1:
		   week="星期一";
		   break;
		  case 2:
		   week="星期二";
		   break;
		  case 3:
		   week="星期三";
		   break;
		  case 4:
		   week="星期四";
		   break;
		  case 5:
		   week="星期五";
		   break;
		  case 6:
		   week="星期六";
		   break;
		  case 0:
		   week="星期日";
		   break;
		 }
		 document.getElementById('weekLabel').innerHTML = week;
		 year = window.sessionStorage.getItem('YEAR');
		 month = window.sessionStorage.getItem('MONTH');
		 day = window.sessionStorage.getItem('DAY');
		 var date = new Date();
		var YY = date.getYear();
		if (YY < 1000) {
			YY += 1900;
		}
		YY = YY.toString();
		var MM = (date.getMonth() + 1).toString();
		MM = MM.length==1?'0'+MM:MM;
		var DD = date.getDate().toString();
		DD = DD.length<2?'0'+DD:DD;
		 if(year > YY||month>MM||day>DD||year == null){	
			get_degrees();
		 }else{
	
			var weather = window.sessionStorage.getItem('WEATHER');
			if(weather){
				var str_degrees = window.sessionStorage.getItem('DEGRESS');
	
				document.getElementById("timeLabel").innerHTML="今日气温："+weather;
				document.getElementById("degrees").innerHTML = str_degrees;
				
				/**时间对了在显示日期*/
				document.getElementById("weekLabel").style.display = "block";
				document.getElementById("dateLabel").style.display = "block";
				document.getElementById("timeLabel").style.display = "block";
				document.getElementById("degrees").style.display = "block";
				/**时间对了在显示日期*/
			}else{
				get_degrees();
			}
		 }
	}catch(e){
		
	}
}

var now_date = ''
var city_obj = null;
var net_obj_time = null;
var day = null;
var month = null ;
var year = null;
function get_degrees(){
    var city_address = 'http://ip.cms.sohu.com/ip.do?type=json';
   /* var weatherData = getCookie('weather');
    console.log('---------cookie,weatherData:' + weatherData + '-----------');
    if (weatherData) {
        console.log('--------get cookie---------');
        var content = argument2;
        var weather_low = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"Low\"\:\"[\S\s]*?\"\,/)[0].replace(/\"Low\"\:\"|\"\,/g, '');
        var weather_high = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"High\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"High\"\:\"|\"\}\,/g, '');
        var weather = weather_low + '/' + weather_high + "℃";
        var weather_img = content.match(/\"Weather\"\:\{\"Desc\"[\S\s]*?\"\}\,/)[0].match(/\"Symbol\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"Symbol\"\:\"|\"\}\,/g, '');
        var weather_logo = "http://news.sohu.com/upload/weather1217/wp" + weather_img[0] + weather_img[1] + ".gif";
        var str_degrees = '<div class="degrees fl">' + weather + '</div><img src="' + weather_logo + '" class="fl">';
        document.getElementById("degrees").innerHTML = str_degrees;
    }
    else {*/
		
			console.log('--------get data---------');
			if (!city_obj) {
				city_obj = wae.create("Net.HttpClient");
			}
			if (!net_obj_time) {
				net_obj_time = wae.create("Net.HttpClient");
			}
			city_obj.getWithAsync(city_address, 30);
			city_obj.oncomplete = function(e){
				
				var argument0 = e.arguments[0].toLocaleString(); //URL
				var argument1 = e.arguments[1].toLocaleString(); //Response Code
				var argument2 = e.arguments[2].toLocaleString(); //content
				var content = argument2;
				var address = content.match(/cid\:[\S\s]*?\,/)[0].replace(/cid\:|\,/g, '');
				var date = new Date();
				var YY = date.getYear();
				if (YY < 1000) {
					YY += 1900;
				}
				YY = YY.toString();
				var MM = (date.getMonth() + 1).toString();
				MM = MM.length==1?'0'+MM:MM;
				var DD = date.getDate().toString();
				DD = DD.length<2?'0'+DD:DD;

				if(year > YY||month>MM||day>DD||year == null){	
				var Response = city_obj.getResponseHeaders(argument0).match(/"Date":"[\S\s]*?"/)[0].replace(/"Date":"|"/g,'');
				var data =Response.split(",")[1].split(" ");
				day = data[1];
				month = data[2];
				switch (month){
				  case 'Jan':
				   month="01";
				   break;
				  case 'Feb':
				   month="02";
				   break;
				  case 'Mar':
				   month="03";
				   break;
				  case 'Apr':
				   month="04";
				   break;
				  case 'May':
				   month="05";
				   break;
				  case 'Jun':
				   month="06";
				   break;
				  case 'Jul':
				   month="07";
				   break;
				  case 'Aug':
				   month="08";
				   break;
				  case 'Sep':
				   month="09";
				   break;
				  case 'Oct':
				   month="10";
				   break;
				  case 'Nov':
				   month="11";
				   break;
				  case 'Dec':
				   month="12";
				   break;
				 }
				year = data[3]
				/*
				*/
				now_date = year+month+day;
				window.sessionStorage.setItem('YEAR',year);
				window.sessionStorage.setItem('MONTH',month);
				window.sessionStorage.setItem('DAY',day);
			}else{
				if(year > YY&&month>MM&&day>DD){
					now_date = year+month+day;
				}else{
					now_date = YY + MM + DD;
				}		
				
			}
			var degrees = "http://m.sohu.com/weather/cms/" + now_date + "/" + address + ".3days.json";
			console.log('------------degrees:' + degrees + '------------');
			net_obj_time.getWithAsync(degrees, 30);
		  //  }
			net_obj_time.oncomplete = function(e){
				var argument0 = e.arguments[0].toLocaleString(); //URL
				var argument1 = e.arguments[1].toLocaleString(); //Response Code
				var argument2 = e.arguments[2].toLocaleString(); //content
				var content = argument2;
				console.log('-----------net_obj_time content:' + content + '-------------');
				var weather_low = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"Low\"\:\"[\S\s]*?\"\,/)[0].replace(/\"Low\"\:\"|\"\,/g, '');
				var weather_high = content.match(/\"Temperature\"\:\{\"Low\"\:\"[\S\s]*?\}\,\"/)[0].match(/\"High\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"High\"\:\"|\"\}\,/g, '');
				var weather = weather_low + '/' + weather_high + "℃";
				var weather_img = content.match(/\"Weather\"\:\{\"Desc\"[\S\s]*?\"\}\,/)[0].match(/\"Symbol\"\:\"[\S\s]*?\"\}\,/)[0].replace(/\"Symbol\"\:\"|\"\}\,/g, '');
				var weather_logo = "http://news.sohu.com/upload/weather1217/wp" + weather_img[0] + weather_img[1] + ".gif";
				//var str_degrees = '<div class="degrees fl">' + weather + '</div><img  height="32" width="32" src="' + weather_logo + '" class="fl">';
				var str_degrees = '<div class="degrees fl"></div><img  height="32" width="32" src="' + weather_logo + '" class="fl">';
				document.getElementById("timeLabel").innerHTML="今日气温："+weather;
				document.getElementById("degrees").innerHTML = str_degrees;
				window.sessionStorage.setItem('WEATHER',weather);
				window.sessionStorage.setItem('DEGRESS',str_degrees);
				//setCookie('weather', content, 1)
				
				/**时间对了在显示日期*/
				document.getElementById("weekLabel").style.display = "block";
				document.getElementById("dateLabel").style.display = "block";
				document.getElementById("timeLabel").style.display = "block";
				document.getElementById("degrees").style.display = "block";
				/**时间对了在显示日期*/
				
			}
		}
}

/*function setCookie(c_name, value, expiredays){
	try {
		var exdate = new Date()
		exdate.setDate(exdate.getDate() + expiredays)
		document.cookie = c_name + "=" + escape(value) +
		((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
		
	}catch(e){
		console.log('----------in setCookies error:'+e.message+'---------');
	}
}

function getCookie(c_name){
	try {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(c_name + "=")
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1
				c_end = document.cookie.indexOf(";", c_start)
				if (c_end == -1) 
					c_end = document.cookie.length
				return unescape(document.cookie.substring(c_start, c_end))
			}
		}
	} 
	catch (e) {
		console.log('----------in getCookies error:' + e.message + '---------');
	}
    
    return ""
}*/

Date.prototype.format = function(format) //author: meizz 
{ 
  var o = { 
    "M+" : this.getMonth()+1, //month 
    "d+" : this.getDate(),    //day 
    "h+" : this.getHours(),   //hour 
    "m+" : this.getMinutes(), //minute 
    "s+" : this.getSeconds(), //second 
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter 
    "S" : this.getMilliseconds() //millisecond 
  } 
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1, 
    (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o)if(new RegExp("("+ k +")").test(format)) 
    format = format.replace(RegExp.$1, 
      RegExp.$1.length==1 ? o[k] : 
        ("00"+ o[k]).substr((""+ o[k]).length)); 
  return format; 
}


function cushion_dateTimeInit(){
	var d = new Date();
    var h = d.getHours();
    var i = d.getMinutes().toString();
	i = i.length<2?'0'+i:i;
    var m = h > 12 ? "PM" : "AM";
    var timeFormat =new Date().format("yyyy.MM.dd")+"  "+ m + " " + h + ":" + i;
    document.getElementById('dateLabel').innerText = timeFormat;
	var week;
	switch (d.getDay()){
	  case 1:
	   week="星期一";
	   break;
	  case 2:
	   week="星期二";
	   break;
	  case 3:
	   week="星期三";
	   break;
	  case 4:
	   week="星期四";
	   break;
	  case 5:
	   week="星期五";
	   break;
	  case 6:
	   week="星期六";
	   break;
	  case 0:
	   week="星期日";
	   break;
	 }
	 document.getElementById('weekLabel').innerHTML = week;

	
	var weather = window.sessionStorage.getItem('WEATHER');
	if(weather){
		var str_degrees = window.sessionStorage.getItem('DEGRESS');

		document.getElementById("timeLabel").innerHTML="今日气温："+weather;
		document.getElementById("degrees").innerHTML = str_degrees;
		
		/**时间对了在显示日期*/
		document.getElementById("weekLabel").style.display = "block";
		document.getElementById("dateLabel").style.display = "block";
		document.getElementById("timeLabel").style.display = "block";
		document.getElementById("degrees").style.display = "block";
		/**时间对了在显示日期 新权限*/
	}
}



var application_obj = null;
//版本
function showVersion(){
	
	if(!application_obj){
		application_obj = wae.create("Application");
	}
	
	document.getElementById('versionName').innerText = 'ver '+application_obj.versionName() + '.' + HTML_PAGE_VERSION;
}
